package cn.itcast.controller;

import cn.itcast.common.BaseContext;
import cn.itcast.common.R;
import cn.itcast.dto.OrderDto;
import cn.itcast.entity.Dish;
import cn.itcast.entity.OrderDetail;
import cn.itcast.entity.Orders;
import cn.itcast.entity.User;
import cn.itcast.service.OrderDetailService;
import cn.itcast.service.OrderService;
import cn.itcast.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private OrderDetailService orderDetailService;


    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page> pageR(int page,int pageSize,String number,String beginTime,String endTime){
        Page<Orders> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.like(number != null,Orders::getNumber,number);
        ordersLambdaQueryWrapper.like(beginTime != null,Orders::getOrderTime,beginTime);
        ordersLambdaQueryWrapper.like(endTime != null, Orders::getCheckoutTime,endTime);
        ordersLambdaQueryWrapper.orderByAsc(Orders::getId);
        orderService.page(page1,ordersLambdaQueryWrapper);

        Page<OrderDto> page2 =new Page<>();
        BeanUtils.copyProperties(page1,page2,"records");
        List<Orders> records = page1.getRecords();
        List<OrderDto> collect = records.stream().map((item) -> {
            OrderDto orderDto = new OrderDto();
            BeanUtils.copyProperties(item, orderDto);
            Long userId = item.getUserId();
            User user = userService.getById(userId);
            String name = user.getName();
            orderDto.setName(name);
            return orderDto;
        }).collect(Collectors.toList());
        page2.setRecords(collect);

        return R.success(page2);
    }

    /**
     * 最新订单历史订单
     * @return
     */
    @GetMapping("/userPage")
    public R<Page> orderPage(int page , int pageSize){
    /*    Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.orderByAsc(Orders::getOrderTime);
        ordersLambdaQueryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId());
        Page<Orders> page1 = orderService.page(ordersPage, ordersLambdaQueryWrapper);
        List<Orders> list = orderService.list(ordersLambdaQueryWrapper);
        Page<OrderDetail> page2 = new Page<>();
        list.stream().map((item) -> {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(item,orderDetail);
            LambdaQueryWrapper<OrderDetail> orderDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            orderDetailLambdaQueryWrapper.eq(OrderDetail::getOrderId,item.getId());
            List<OrderDetail> list1 = orderDetailService.list(orderDetailLambdaQueryWrapper);
            item
        }).collect(Collectors.toList());

        return R.success(page1);*/
        //构造分页构造器
        Page<Orders> pageInfo = new Page(page, pageSize);
        //按下单日志排序
        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("order_time");
        //查询当前登陆用户的订单
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        orderService.page(pageInfo,queryWrapper);

        //查询订单明细
        pageInfo.getRecords().stream().map(item -> {
            List<OrderDetail> details =
                    orderDetailService.list(Wrappers.<OrderDetail>lambdaQuery().eq(OrderDetail::getOrderId, item.getId()));
            item.setOrderDetails(details);
            return item;
        }).collect(Collectors.toList());
        return R.success(pageInfo);
    }

    /**
     * 修改订单状态
     * @return
     */
    @PutMapping
    public R<String> modify(@RequestBody Orders orders) {
        orderService.updateById(orders);
        return R.success("修改成功");
    }
}